/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */
#pragma once

#include "ResourceActive.h"
#include "Manager.h"
#include <filesystem/String.hpp>
#include <vector>

namespace liba
{
namespace resource
{

/** This class uses second thread to read data into {@link data}
	@warning you should not call any method until {@link is_ready} 
	returns true. */
class ActiveMultithreaded : public ResourceActive
{
	Resource * res;
	int pos;
	filesystem::String name;
	bool valid;
	std::vector<char> acquired_data;
	volatile bool ready;

public:
	class Creator : virtual public Manager::Creator
	{
	public:
		Creator();
		~Creator();
		void LifeCycle();
	};
	explicit ActiveMultithreaded(const filesystem::String & name);
	virtual ~ActiveMultithreaded();
	virtual int read(void * data, int count);
	virtual int skip(int count);
	virtual bool is_valid()const;

	virtual int get_size()const;
	virtual int get_position()const;
	virtual void reset(int position = 0);
	virtual const void * get_data()const;
	virtual bool is_ready()volatile const;
private:
	friend class ActiveMultithreadedLoader;
	void internal_load();
};

} // namespace resource
} // namespace liba
using namespace liba;

